package com.ruyuan.dependency.product.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruyuan.dependency.product.entity.Sku;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultType;
import org.apache.ibatis.annotations.Update;

/**
 * @author zhonghuashishan
 */
public interface SkuMapper extends BaseMapper<Sku> {

    @Update("UPDATE sku SET current_stock = current_stock - #{count}, locked_stock = locked_stock + #{count} " +
            "WHERE id = #{id} AND current_stock > #{count}")
    @ResultType(Integer.class)
    int lockStock(@Param("id") Long id, @Param("count") Integer count);

    @Update("UPDATE sku SET current_stock = current_stock + #{count}, locked_stock = locked_stock - #{count} " +
            "WHERE id = #{id} AND locked_stock > #{count}")
    @ResultType(Integer.class)
    int releaseStock(@Param("id") Long id, @Param("count") Integer count);
}